﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Markup;
using CoreGallery.Addins.Contracts;
using CoreGallery.Core.AddinMgr;

//[assembly: XmlnsDefinition("http://coregallery.net", "CoreGallery.Core")]
namespace CoreGallery.Core.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        AddinManager addinManager;

        public MainWindow()
        {
            InitializeComponent();

            addinManager = AddinManager.GetInstance();
            CoreGallery.Core.Application.App.Exit += new ExitEventHandler(App_Exit);
            Init();

            IList<object> photos = new List<object>();
            photos.Add("1");
            photos.Add("2");
            photos.Add("3");
            photos.Add("4");
            photos.Add("5");

            ThumbViewer.PhotoList = photos;
        }
        
        void App_Exit(object sender, ExitEventArgs e)
        {
            UnloadAllAddIns();
            GC.WaitForPendingFinalizers();
        }

        public void Init()
        {
            addinManager.LoadAddIns();

            var sortedAddins =
                from a in addinManager.LoadedAddins
                where a.IsEnabled == true
                orderby a.AddinInterface.Name, a.AddinName
                group a by a.AddinInterface.Name into g
                select new { AddinType = g.Key, Addins = g };

            foreach (var addinGroup in sortedAddins)
            {
                if (addinGroup.Addins.Count() == 0)
                {
                    this.AddInMenuList.Items.Add(new MenuItem() { Header = "No " + addinGroup.AddinType + " addins found" });
                }

                foreach (var addin in addinGroup.Addins)
                {
                    InitAddIn(addin);
                }

                this.AddInMenuList.Items.Add(new Separator());
            }

            MenuItem addinManagerMenuItem = new MenuItem();
            addinManagerMenuItem.Header = "Add-Ins Manager";
            addinManagerMenuItem.Click += new RoutedEventHandler(addinManagerMenuItem_Click);
            this.AddInMenuList.Items.Add(addinManagerMenuItem);
        }

        void addinManagerMenuItem_Click(object sender, RoutedEventArgs e)
        {
            AddinManagerWindow win = new AddinManagerWindow();
            win.SetItemSource(addinManager);
            win.ShowDialog();
        }

        /// <summary>
        /// Setup each addin as necessary
        /// </summary>
        /// <param name="addin"></param>
        void InitAddIn(AddinInfo addin)
        {
            addinManager.ActivateAddin(addin);

            MenuItem menuItem = new MenuItem();
            menuItem.Header = addin.AddinInstance.Name;
            menuItem.Click += new RoutedEventHandler(addinMenuItem_Click);
            menuItem.Tag = addin;

            this.AddInMenuList.Items.Add(menuItem);
        }

        void addinMenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(((AddinInfo)((MenuItem)sender).Tag).AddinInstance.AboutText);
        }

        void UnloadAllAddIns()
        {
            // Remove from UI
            this.AddInMenuList.Items.Clear();

            // Remove from memory collection
            addinManager.UnloadAllAddIns();
        }
 
    }
}